package com.csw.searchRecSys.controller;

import com.csw.searchRecSys.entity.Poem;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("h")
public class HotWordsController {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private ElasticsearchRestTemplate elasticsearchRestTemplate;

	@RequestMapping("hotwords")
	@ResponseBody
	public List<Poem> hotwords(String hotwords) {
		HashOperations<String, Object, Object> ss = stringRedisTemplate.opsForHash();
		String o = (String) ss.get("maps", hotwords);
		if (o == null) {
			ss.put("maps", hotwords, "1");
		} else {
			Double o1 = Double.parseDouble(o) + 1;
			ss.put("maps", hotwords, o1.toString());
		}
		/**
		 *
		 */
		/*3. 查询*/
		NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
				.withQuery(QueryBuilders.multiMatchQuery(hotwords, "id", "name", "content", "author", "type", "authordes"))
				.withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC))
				/*.withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))*/
				.withPageable(PageRequest.of(0, 10))
				.withHighlightFields(
						new HighlightBuilder.Field("name").preTags("<span style='color:red'>").postTags("</span>"),
						new HighlightBuilder.Field("content").preTags("<span style='color:red'>").postTags("</span>"),
						new HighlightBuilder.Field("authordes").preTags("<span style='color:red'>").postTags("</span>"))
				.build();

		/* 4.处理查询结果*/
		SearchHits<Poem> articles = elasticsearchRestTemplate.search(nativeSearchQuery, Poem.class);
		/*得到查询结果返回的内容*/
		List<org.springframework.data.elasticsearch.core.SearchHit<Poem>> searchHits = articles.getSearchHits();
		/* 设置一个需要返回的实体类集合*/
		List<Poem> discussPosts = new ArrayList<>();
		for (org.springframework.data.elasticsearch.core.SearchHit<Poem> searchHit : searchHits) {
			Map<String, List<String>> highLightFields = searchHit.getHighlightFields();
			/*将高亮的内容填充到content中*/
			searchHit.getContent().setName(highLightFields.get("title") == null ? searchHit.getContent().getName() : highLightFields.get("name").get(0));
			searchHit.getContent().setContent(highLightFields.get("content") == null ? searchHit.getContent().getContent() : highLightFields.get("content").get(0));
			searchHit.getContent().setAuthordes(highLightFields.get("authordes") == null ? searchHit.getContent().getAuthordes() : highLightFields.get("authordes").get(0));
			/*放到实体类中*/
			discussPosts.add(searchHit.getContent());

		}

		return discussPosts;

	}

	@RequestMapping("add")
	@ResponseBody
	public Map<Object, Object> qa() {
		HashOperations<String, Object, Object> ss = stringRedisTemplate.opsForHash();

		return ss.entries("maps");
		/*
		 * Map<Object,Object> map=new LinkedHashMap<>(); ZSetOperations<String,String>
		 * zset=stringRedisTemplate.opsForZSet(); Set<String>
		 * set=zset.reverseRange("zset",0,-1); for (String s : set) {
		 * map.put(s,zset.score("zset",s));
		 *
		 * } System.out.println("map;;///////"+map); return map;
		 */
	}

}
